package com.jy.api.controller.admin;

import com.jy.api.dao.CouponDao;
import com.jy.api.dao.StoreConfigDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.dao.StoreRegionConfigDao;
import com.jy.api.domain.Coupon;
import com.jy.api.domain.Store;
import com.jy.api.domain.StoreConfig;
import com.jy.api.domain.StoreRegionConfig;
import com.jy.api.service.StoreConfigService;
import com.jy.api.signUpReq.admin.StoreRegionConfigUpdateReq;
import com.jy.api.signUpReq.admin.StoresConfigSignUpReq;
import com.jy.api.util.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2018/1/19.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/admin/", produces = "application/json;charset=utf-8")
public class StoreConfigController {

    @Autowired
    StoreDao storeDao;

    @Autowired
    CouponDao couponDao;

    @Autowired
    StoreConfigService storeConfigService;

    @Autowired
    StoreConfigDao storeConfigDao;
    @Autowired
    StoreRegionConfigDao storeRegionConfigDao;

    /**
     * 获取店铺会员新增赠送优惠卷
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "coupon/stores-config", method = RequestMethod.GET)
    public ResponseEntity getConfig(@RequestParam(name = "storeId") String storeId) {
        try {
            Map map = new HashMap();
            StoreConfig storeConfig = storeConfigDao.findByStoreId(storeId);
            if (storeConfig != null && StringUtils.isNotBlank(storeConfig.getGiveCouponId())) {
                Coupon coupon = couponDao.findOne(storeConfig.getGiveCouponId());
                if (coupon != null) {
                    map.put("couponId", storeConfig.getGiveCouponId());
                    map.put("title", coupon.getTitle());
                }
            }
            return new ResponseEntity(map, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("initClientUrl is error =>", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 店铺设置修改接口
     *
     * @param signUpReq
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "stores-config", method = RequestMethod.POST)
    public ResponseEntity updateConfig(@Valid @RequestBody StoresConfigSignUpReq signUpReq,
                                       BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST);
            }
            Store store = storeDao.findOne(signUpReq.getStoreId());
            if (store == null) {
                return new ResponseEntity("设置失败！店铺信息不存在！", HttpStatus.BAD_REQUEST);
            }
            if (StringUtils.isNotBlank(signUpReq.getGiveCouponId())) {
                Coupon coupon = couponDao.findOne(signUpReq.getGiveCouponId());
                if (coupon == null) {
                    return new ResponseEntity("设置失败！优惠卷信息不存在！", HttpStatus.BAD_REQUEST);
                }
            }

            storeConfigService.updateStoreConfig(signUpReq);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("initClientUrl is error =>", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取店铺机器监听配置
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "store-region-config", method = RequestMethod.GET)
    public ResponseEntity getStoreRegionConfig(@RequestParam(name = "storeId") String storeId) {
        return new ResponseEntity(storeRegionConfigDao.findByStoreId(storeId), HttpStatus.OK);
    }

    /**
     * 更新店铺机器监听配置
     *
     * @param signUpReq
     * @return
     */
    @RequestMapping(value = "store-region-config", method = RequestMethod.POST)
    public ResponseEntity updateStoreRegionConfig(@Valid @RequestBody StoreRegionConfigUpdateReq signUpReq) {
        try {
            Store store = storeDao.findOne(signUpReq.getStoreId());
            if (store == null) {
                return new ResponseEntity("设置失败！店铺信息不存在！", HttpStatus.BAD_REQUEST);
            }
            StoreRegionConfig regionConfig = storeRegionConfigDao.findByStoreId(signUpReq.getStoreId());
            if (null == regionConfig) {
                regionConfig = new StoreRegionConfig(UuidUtil.getUuid(), signUpReq.getStoreId(), false, false, signUpReq.getMinutes(), signUpReq.getMessage());
            } else {
                regionConfig.setVoiceNotify(signUpReq.isVoiceNotify());
                regionConfig.setDateOpen(signUpReq.isDateOpen());
                regionConfig.setMinutes(signUpReq.getMinutes());
                regionConfig.setMessage(signUpReq.getMessage());
            }
            storeRegionConfigDao.save(regionConfig);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("initClientUrl is error =>", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
